package com.imooc.poker_game;

import com.imooc.poker_game.exception.WrongCommandException;

import java.util.*;

/**
 * 比较两位玩家手中的扑克牌
 * 1》 比较两人手中牌的点数，点数大赢
 * 2》 点数相同时，比较花色， 黑 红 梅 方
 *
 * 发牌规则：从扑克第一张开始，一人一张，每人两张
 */
public class Game {
    /**
     * 游戏人数
     */
    public int numberOfGamer = 2;
    /**
     * 每人几张牌
     */
    public int piecesPerGamer = 2;

    public Game() {

    }

    /**
     * 王佳列表
     * @param numberOfGamer int
     * @return
     */
    public List<Player> gamePlayerInfo(int numberOfGamer) {
        List<Player> players = new ArrayList<Player>();
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < numberOfGamer; i++) {
            System.out.println("请输入玩家" + i + "号的信息！");
            System.out.println("编号：");
            String id = scanner.next();
            // 对id进行判断
            System.out.println("姓名：");
            String name = scanner.next();
            try {
                Player player = new Player(id, name);
                players.add(player);
                System.out.println("---------------------");
            } catch (WrongCommandException e) {
                System.out.println("err: " + e.getMessage());
                i --;
            }
        }

        return players;
    }

    /**
     * 发牌
     * @param poker
     * @param players
     */
    public void dealing(Poker poker, List<Player> players) {
        // 每个人发几张牌
        for (int i = 1; i <= this.piecesPerGamer; i++) {
            // 给玩家依次发牌
            for (int num = 0; num < this.numberOfGamer; num ++) {
                int index = (i - 1) * this.numberOfGamer + num;
                System.out.println("----玩家：" + players.get(num).name + "--拿牌");
                System.out.println("----牌面为：" + index);
                players.get(num).addPiece(poker.pokerPieces.get(index));
            }
        }
    }

    /**
     * 获胜
     * @param players
     * @return
     */
    public Player winner(List<Player> players) {
        List<PokerPiece> bestPieces = new ArrayList<PokerPiece>();
        for (Player player:players) {
            PokerPiece best = player.bestPiece();
            bestPieces.add(best);
            System.out.println("players = " + player.name + "最好的牌为：" + player.bestPiece());
        }

        Collections.sort(bestPieces);
        PokerPiece bestBokerPiece = bestPieces.get(0);
        for (Player player:  players) {
            if (player.pokerPieces.contains(bestBokerPiece))
                return player;
        }
        return null;
    }

    public static void main(String[] args) {
        // 新建牌局
        Game newGame = new Game();
        // 获取52张扑克 -
        Poker poker = new Poker();
        // 洗牌0
        poker.shuffle();
        // 玩家进场
        List<Player> players = newGame.gamePlayerInfo(newGame.numberOfGamer);
        for (Player player:players) {
            System.out.println("欢迎玩家：" + player.name);
        }
        // 开始牌局
        System.out.println("------- 开始牌局 -------");
        // 发牌
        System.out.println("------- 开始发牌 -------");
        newGame.dealing(poker, players);
        System.out.println("------- 结束发牌 -------");
        // 比较大小
        System.out.println("------- 开始游戏 -------");
        Player winner = newGame.winner(players);
        System.out.println("----- 玩家 " + winner.name + " 获胜！-------");
        // 展示结果
        for (Player player: players) {
            System.out.println(player.name + " : " + player.show());
        }
    }
}
